/*
 * INSANE - Interactive Structural Analysis Environment
 *
 * Copyright (C) 2003-2006
 * Universidade Federal de Minas Gerais
 * Escola de Engenharia
 * Departamento de Engenharia de Estruturas
 * 
 * Author's email :    insane@dees.ufmg.br
 * Author's website :  http://www.dees.ufmg.br/insane
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package br.ufmg.dees.insane.commons.view;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import java.util.ResourceBundle;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.commons.command.CommandHolder;
import br.ufmg.dees.insane.commons.command.RedoCommand;
import br.ufmg.dees.insane.commons.command.UndoCommand;
import br.ufmg.dees.insane.commons.controller.Controller;

/**
 * A class representing an Internal Interface shown in an Interface 
 * 
 * @author Samuel Penna, Flavio Fonseca
 * @since September 2006
 */
public abstract class InternalInterface extends JInternalFrame implements ActionListener, Observer {
	
	protected Interface owner;
	protected IView view;
	protected JPanel drawArea = new JPanel();
	protected Controller controller;
	protected ResourceBundle bundle;
	
	protected UndoCommand undoCmd = new UndoCommand(this);
	protected RedoCommand redoCmd = new RedoCommand(this);
	
	/**
	 * The constructor method. <br>
	 * Calls the init() method throwing an exception.
	 */
	public InternalInterface(ResourceBundle bundle) {
		this.bundle = bundle;
	}

	/**Sets the fonts used by this InternalInterface
	*/
	public void setFonts() {
		
		try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            
            Font fontCurrent = UIManager.getFont("Button.font");
            String name = fontCurrent.getName();
            
            //Creates similar font with the specified style and size
            FontUIResource fontResourceNew  = new FontUIResource(name, Font.PLAIN, 12);
            
            //Sets all items fonts
            UIManager.put("Button.font", fontResourceNew);
            UIManager.put("CheckBox.font", fontResourceNew);
            UIManager.put("CheckBoxMenuItem.font", fontResourceNew);
            UIManager.put("ColorChooser.font", fontResourceNew);
            UIManager.put("ComboBox.font", fontResourceNew);
            UIManager.put("DesktopIcon.font", fontResourceNew);
            UIManager.put("EditorPane.font", fontResourceNew);
            UIManager.put("InternalFrame.font", fontResourceNew);
            UIManager.put("Label.font", fontResourceNew);
            UIManager.put("List.font", fontResourceNew);
            UIManager.put("MenuBar.font", fontResourceNew);
            UIManager.put("Menu.font", fontResourceNew);
            UIManager.put("MenuItem.font", fontResourceNew);
            UIManager.put("OptionPane.font", fontResourceNew);
            UIManager.put("PasswordField.font", fontResourceNew);
            UIManager.put("PopupMenu.font", fontResourceNew);
            UIManager.put("ProgressBar.font", fontResourceNew);
            UIManager.put("RadioButton.font", fontResourceNew);
            UIManager.put("RadioButtonMenuItem.font", fontResourceNew);
            UIManager.put("ScrollPane.font", fontResourceNew);
            UIManager.put("TabbedPane.font", fontResourceNew);
            UIManager.put("TableHeader.font", fontResourceNew);
            UIManager.put("TextArea.font", fontResourceNew);
            UIManager.put("TextField.font", fontResourceNew);
            UIManager.put("TextPane.font", fontResourceNew);
            UIManager.put("TitledBorder.font", fontResourceNew);
            UIManager.put("ToggleButton.font", fontResourceNew);
            UIManager.put("ToolBar.font", fontResourceNew);
            UIManager.put("ToolTip.font", fontResourceNew);
            UIManager.put("Tree.font", fontResourceNew);
            UIManager.put("Viewport.font", fontResourceNew);

        }
        catch (Exception e){}
	}

	/** Returns the current view.
	*  @return The current view.
	*/
	public IView getView() {
		return this.view;
	}
	
	/** Sets the initial Interface. */
	public abstract void setInitialInterface();


	/** Adds a Command to the undoList.
	 * @param cmd a the desired command
	 **/
	public void addToUndo(Command cmd) {
		
	}

	/** Adds a Command to the redoList.
	 * @param cmd a the desired command
	 */
	public void addToRedo(Command cmd) {
	
	}

	/** Disables the redo command.*/
	public void nothingToRedo() {
		
	}

	/** Disables the undo command.*/
	public void nothingToUndo() {
		
	}

	/** Method which executes the desired action to each command.*/
	public void actionPerformed(ActionEvent evt) {
		CommandHolder obj = (CommandHolder)evt.getSource();
		obj.getCommand().execute();
	}
	
	/**Updates this InternalInterface
	*/
	public void update(Observable arg0, Object arg1) {}
	
	/**Returns this InternalInterface's parent
	*@return This InternalInterface's parent
	*/
	public Interface getOwner() {
		return owner;
	}
	
	/**Set this InternalInterface's owner
	*@param parent This InternalInterface's owner
	*/
	public void setOwner(Interface owner) {
		this.owner = owner;
	}
	
	/**Set this InternalInterface's Controller
	*@param controller This InternalInterface's Controller
	*/
	public void setController(Controller controller){
		this.controller = controller;
	}
	
	/**Returns this InternalInterface's Controller
	*@return This InternalInterface's Controller
	*/
	public Controller getController(){
		return controller;
	}
	
	/**Returns this InternalInterface's ResourceBundle
	*@return This InternalInterface's ResourceBundle
	*/
	public ResourceBundle getResourceBundle(){
		return bundle;
	}
	
	/**Set this InternalInterface's ResourceBundle
	*@param bundle This InternalInterface's ResourceBundle
	*/
	public void setResourceBundle(ResourceBundle bundle){
		this.bundle = bundle;
	}
	
	/**
	 * Writes the message in this InternalInterface output
	 * @param message The message to be written
	 */
	public abstract void addOutputText(String message);
	
	/**
	 * Returns this InternalInterface's DrawingArea
	 * @return this InternalInterface's DrawingArea
	 */
	public abstract DrawingArea getDrawing();
	
	/** Returns the UndoCommand of this InternalInterface.
	*@return The UndoCommand of this InternalInterface.
	*/
	public UndoCommand getUndoCommand() {
		return undoCmd;
	}
	
	/** Returns the RedoCommand of this InternalInterface.
	*@return The RedoCommand of this InternalInterface.
	*/
	public RedoCommand getRedoCommand() {
		return redoCmd;
	}
}